///
//  EmoticonToolView.swift
//
//  Created by yang on 2017/11/12.
//  Copyright © 2017年 成品家（北京）网络科技有限公司. All rights reserved.
//

import UIKit

//定义协议方法
@objc protocol EmoticonToolViewDelegate: NSObjectProtocol
{
    @objc optional func emoticonToolViewDidSelectedItemIndex(toolView: EmoticonToolView, index: Int)
}
class EmoticonToolView: UIView {
    
    //选中分组按钮对应的index
    var selectedIndex: Int = 0
    {
        didSet
        {
            for btn in subviews as! [UIButton]
            {
                btn.isSelected = false
            }
            //选中的分组按钮状态变化
            (subviews[selectedIndex] as! UIButton).isSelected = true
        }
    }
    
    //定义代理
    weak var delegate: EmoticonToolViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.frame = CGRect(x: 0, y: 218, width: UIScreen.main.bounds.width, height: 40)
        //初始化控件
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //布局子视图
    override func layoutSubviews() {
        super.layoutSubviews()
        let width = bounds.width/CGFloat(subviews.count)
        
        let rect = CGRect(x: 0, y: 0, width: width, height: bounds.height)
        for (i, btn) in subviews.enumerated()
        {
            btn.frame = rect.offsetBy(dx: CGFloat(i) * width, dy: 0)
        }
    }
    
    //添加item的监听方法
    @objc private func clickItem(button: UIButton)
    {
        delegate?.emoticonToolViewDidSelectedItemIndex?(toolView: self, index: button.tag)
    }
}

private extension EmoticonToolView
{
    //初始化控件
    func setupUI()
    {
        let manager = EmoticonManager.shared
        
        for (i, packageModel) in manager.packages.enumerated()
        {
            let btn = UIButton()
            btn.setTitle(packageModel.groupName, for: .normal)
            btn.setTitleColor(UIColor.white, for: .normal)
            btn.setTitleColor(UIColor.darkGray, for: .selected)
            btn.setTitleColor(UIColor.darkGray, for: .highlighted)

            //设置btn的tag值
            btn.tag = i
            //添加按钮的监听方法
            btn.addTarget(self, action: #selector(clickItem), for: .touchUpInside)
            //设置背景图片
            let imageNameNormal = "compose_emotion_table_\(packageModel.bgImageName ?? "")_normal"
            let imageNameSelected = "compose_emotion_table_\(packageModel.bgImageName ?? "")_selected"
            var imageNormal = UIImage(named: imageNameNormal, in: manager.bundle, compatibleWith: nil)
            var imageSelected = UIImage(named: imageNameSelected, in: manager.bundle, compatibleWith: nil)
            //拉伸图片
            let size = imageNormal?.size ?? CGSize()
            let inset = UIEdgeInsetsMake(size.height * 0.5, size.width * 0.5, size.height * 0.5, size.width * 0.5)
            imageNormal = imageNormal?.resizableImage(withCapInsets: inset)
            imageSelected = imageSelected?.resizableImage(withCapInsets: inset)
            btn.setBackgroundImage(imageNormal, for: .normal)
            btn.setBackgroundImage(imageSelected, for: .selected)
            
            btn.sizeToFit()
            addSubview(btn)
        }
        //默认选中第一个按钮
        (subviews[0] as! UIButton).isSelected = true
    }
}
